/* 
 *  This file is part of hmvcBlog-bloglist.
 *
 *  hmvcBlog-bloglist is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  hmvcBlog-bloglist is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with hmvcBlog-bloglist.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.googlecode.hmvc4gwt.example.hmvcblog.frontend.bloglist.client;

import com.google.gwt.user.client.History;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.bloglist.client.bloglist.BloglistController;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.bloglist.client.bloglist.BloglistModel;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.bloglist.client.bloglist.BloglistView;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.bloglist.client.bloglist.events.in.ShowBloglistViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.historytoken.client.HistoryToken;
import com.googlecode.hmvc4gwt.framework.client.module.AModuleControllerRegistry;
import com.googlecode.hmvc4java.component.Component;
import com.googlecode.hmvc4java.event.IEvent;

/**
 * 
 *
 * @author jens.meiss
 */
public class Bloglist extends AModuleControllerRegistry {

	public void handleEventImpl(IEvent event) {
	}

	public void initImpl() {
		
		if (History.getToken() == null || History.getToken().equals("")) {
			History.newItem(HistoryToken.bloglist);
		} else {
			History.newItem(History.getToken());
		}
		
	}

	public void onHistoryChanged(String historyToken) {
		
		if (historyToken.equals(HistoryToken.bloglist)) {
			showBloglist();
		}
		
	}

	private void showBloglist() {
		
		if (!getComponent().containsChildComponent(Bloglist.class)) {
			addChildComponent(new Component(new BloglistModel(), new BloglistView(), new BloglistController()));
		}
		
		fireEventDown(new ShowBloglistViewEvent());
	}

	public void terminateImpl() {
	}

}
